<template>
  <div class="qtModels">
   <qtHeader fatherTitle="资产型号" fatherCreate="创建新的" @tell='getName'></qtHeader>
    <el-row class="qtContent">
      <el-col :span="24">
        <el-table :data="tableData" stripe style="width: 100%" height="778px" v-loading="loading">
          <el-table-column prop="modelsName" label="名称" width="120"></el-table-column>
          <el-table-column prop="manufacturersName" label="制造商"></el-table-column>
          <el-table-column prop="model" label="型号"></el-table-column>
          <el-table-column prop="assetsTotal" label="资产"></el-table-column>
          <el-table-column prop="depreciationName" label="折旧"></el-table-column>
          <el-table-column  label="保修">
            <template slot-scope="scope">{{scope.row.depreciationGuarantee}}</template>
          </el-table-column>
          <el-table-column prop="notes" label="笔记"></el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button size="mini" type="warning" @click="handleEdit(scope.row)">
                <i class="el-icon-edit"></i>
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                :disabled="scope.row.assetsTotal > 0 ? true:false"
                :title="scope.row.assetsTotal > 0 ?'已关联资产,禁止删除':''"
              >
                <i class="el-icon-delete"></i>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage1"
          :page-sizes="[15, 25, 35, 45]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-col>
    </el-row>
     <!-- 添加弹框 -->
    <el-dialog  :visible.sync="dialogFormVisible" width="800px"  center custom-class="dialogClass" @close="cancel">
      <el-form :model="form" ref="dataform">
         <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="制造商" :label-width="formLabelWidth" prop="manufacturers">
          <el-select v-model="form.manufacturers" placeholder="请选择制造商">
            <el-option v-for="(item,index) in manufacturers" :key="index" :value="item.id" :label="item.name"></el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="型号" :label-width="formLabelWidth" prop="model">
          <el-input v-model="form.model" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="折旧" :label-width="formLabelWidth" prop="depreciation">
          <el-select v-model="form.depreciation" placeholder="请选择折旧">
            <el-option v-for="(item,index) in depreciation" :key="index" :value="item.id" :label="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="笔记" :label-width="formLabelWidth" prop="notes">
          <el-input v-model="form.notes" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import qtHeader from '@/components/header.vue'
import { getModels, getManufacturersName, getDepreciation, addModels, delModels, checkModels } from '@/api/index.js'
export default {
  data () {
    return {
      tableData: [],
      currentPage1: 1,
      total: 10,
      page: 1,
      pageSize: 15,
      loading: false,
      dialogFormVisible: false,
      form: {
        depreciation: '',
        name: '',
        model: '',
        manufacturers: '',
        notes: ''
      },
      formLabelWidth: '200px',
      row: '',
      assetsId: '',
      depreciation: [],
      manufacturers: []
    }
  },
  components: {
    qtHeader
  },
  mounted () {
    this.getModels()
  },
  methods: {
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getModels()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.getModels()
    },
    getModels () {
      getModels({ page: this.page, pageSize: this.pageSize }).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.data
          for (var key in this.tableData) {
            if (!this.tableData[key].assetsTotal) {
              this.tableData[key].assetsTotal = 0
            }
          }
          this.total = Number(res.data.total.total)
        }
      })
    },
    getName (name) {
      this.dialogFormVisible = name
      // 弹窗的制造商
      getManufacturersName().then(res => {
        if (res.data.code === 200) {
          this.manufacturers = res.data.data
        }
        // console.log(this.manufacturers)
      })
      // 弹窗的折旧
      getDepreciation().then(res => {
        if (res.data.code === 200) {
          this.depreciation = res.data.data
        }
        // console.log(this.depreciation)
      })
    },
    confirm () {
      var obj = {
        depreciation: this.form.depreciation,
        name: this.form.name,
        model: this.form.model,
        manufacturers: this.form.manufacturers,
        notes: this.form.notes
      }
      if (this.assetsId) {
        // 编辑资产
        obj.id = this.assetsId
        if (this.row.modelsName !== this.form.name) {
          obj.name = this.form.name
        } else {
          obj.name = this.row.modelsName
        }
        if (this.row.notes !== this.form.notes) {
          obj.notes = this.form.notes
        } else {
          obj.notes = this.row.notes
        }
        if (this.row.model !== this.form.model) {
          obj.model = this.form.model
        } else {
          obj.model = this.row.model
        }
        if (!isNaN(this.form.manufacturers)) {
          console.log(this.form.manufacturers)
          obj.manufacturers = this.form.manufacturers
        } else {
          obj.manufacturers = this.row.manufacturersId
        }
        if (!isNaN(this.form.depreciation)) {
          obj.depreciation = this.form.depreciation
        } else {
          obj.depreciation = this.row.depreciationId
        }
        // console.log(obj)
        checkModels(obj).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.msg)
            this.getModels()
          } else {
            this.$message.error(res.data.msg)
          }
        })
      } else {
        // 新建资产
        addModels(obj).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.msg)
            this.getModels()
          } else {
            this.$message.error(res.data.msg)
          }
        })
      }

      this.dialogFormVisible = false
    },
    cancel () {
      this.assetsId = ''
      this.$refs.dataform.resetFields()
      for (var key in this.form) {
        this.form[key] = ''
      }
      this.dialogFormVisible = false
    },
    handleEdit (row) {
      this.getName(name)
      this.row = row
      console.log(row)
      this.assetsId = row.id
      this.form.depreciation = row.depreciationName
      this.form.name = row.modelsName
      this.form.model = row.model
      this.form.manufacturers = row.manufacturersName
      this.form.notes = row.notes
    },
    handleDelete (row) {
      console.log(row)
      this.$confirm('是否删除资产?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delModels(row.id).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.msg)
          } else {
            this.$message.error(res.data.msg)
          }
          // 判断当删除的数据为当前页中的最后一条数据，刷新页面的时候需要把当前页码数减1
          if (this.total / this.pageSize - parseInt(this.total / this.pageSize) <= 0.1) {
            this.page = this.page - 1
          }
          this.getModels()
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.qtModels {
  padding: 10px;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.qtTitle {
  font-size: 25px;
  text-align: left;
  line-height: 1;
}
.qtButton {
  margin-top: 5px;
  text-align: right;
  padding-right: 20px;
  .qtCreateAsset {
    background-color: #3c8dbc;
    padding: 5px 10px;
    color: #fff;
    border-radius: 5px;
  }
}
</style>
